package sma.agents;

import javax.swing.JComponent;
import org.janusproject.kernel.crio.core.GroupAddress;
import org.janusproject.kernel.status.Status;
import org.janusproject.kernel.status.StatusFactory;
import sma.agents.bodies.DecisiveAgentBody;
import sma.environment.LocalEnvironment;
import sma.organizations.RealEnvironmentOrga;
import sma.organizations.VirtualEnvironmentOrga;
import sma.organizations.roles.Decisive;
import sma.organizations.roles.DecisiveMassCenter;
import appli.utils.Config;

/**
 * DecisiveAgent : 
 * they have to find a way to go to a goalAgent by avoiding obstacles
 * Vehicule orders are deduced by all decisive agent position.
 * they are all into the virtual organization
 * 
 * @author Etienne
 * @version 0.3
 * @since 0.1
 */
public class DecisiveAgent extends StandartAgent {
		
	
	/**
	 * Constructor. 
	 * 
	 * @param  _environment : local environnement 
	 * @param _groupe telemeter group of the agent
	 * @see LocalEnvironment
	 */
	public DecisiveAgent(sma.environment.LocalEnvironment _environment, int _groupe) {
		
		this.environment = _environment;
		
		this.groupe = _groupe;

		//Affect a body
		this.body = new DecisiveAgentBody();

		if (Config.DEBUG == true)
			System.out.println("Decisive Agent " + this.getName() + " operational");
	}
	

	/**
	 * activate the agent in order it can live
	 * Add role and place the agent into an organization
	 * 
	 * @param parameters no parameters needed
	 */
	@Override
	public Status activate(Object... parameters) {
		super.init((JComponent) parameters[0]);
		
		if (Config.DEBUG == true)
			System.out.println("Agent Decisive " + this.getName() + " activé");
		
		//Get virtualEnvironment address
		GroupAddress virtualEnvGA = getOrCreateGroup(VirtualEnvironmentOrga.class);

		//Affect decisive role
		if (requestRole(Decisive.class, virtualEnvGA, this.environment, this.body, this.groupe,
				(Double) parameters[1], (Double) parameters[2], (Double) parameters[3], (Double) parameters[4])) {
			if (Config.DEBUG == true)
				System.out
						.println("DecisiveAgent belong to Decisive role into VirtualEnvironment group");
		} else if (Config.DEBUG == true)
			System.out
			.println("DecisiveAgent can't belong to Decisive role into VirtualEnvironment group");

		//Get reallEnvironment address
		GroupAddress realEnvGA = getOrCreateGroup(RealEnvironmentOrga.class);

		//Affect DecisiveMassCenter role
		if (requestRole(DecisiveMassCenter.class, realEnvGA, this.environment,
				this.body)) {
			if (Config.DEBUG == true)
				System.out
						.println("DecisiveAgent belong to DecisiveMassCenter role into RealEnvironment group");
			return StatusFactory.ok(this);
		} else if (Config.DEBUG == true)
			System.out
			.println("DecisiveAgent can't belong to DecisiveMassCenter role into RealEnvironment group");

		return StatusFactory.cancel(this);
	}

	/**
	 * agent death
	 */
	@Override
	public Status end() {
		if (Config.DEBUG == true)
			print("Decisive Agent : I'm dead...");
		return StatusFactory.ok(this);
	}

}
